package com.patMall.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.patMall.entity.AnswerProblem;
import com.patMall.entity.Commodity;
import com.patMall.entity.User;
import com.patMall.service.AnswerProblemService;
import com.patMall.service.CommodityService;
import com.patMall.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 750909637@qq.com
 * @PackageName patMall
 * @Package com.patMall.controller
 * @Date 2024/1/19 12:42
 * @Version 1.0
 */
@Controller
@RequestMapping("answerProblem")
public class AnswerProblemController {
    @Resource
    private AnswerProblemService answerProblemService;
    @Resource
    private UserService userService;
    @Resource
    private CommodityService commodityService;

    @PostMapping("getAnswerProblemByCommodityId")
    @ResponseBody
    public Object getAnswerProblemByCommodityId(Integer commodityId) {
        Map map = new HashMap();
        List<AnswerProblem> problem = answerProblemService.selectAnswerProblemByCommodityId(commodityId);
        for (AnswerProblem answerProblem: problem) {
            List<AnswerProblem> answer = answerProblemService.selectAnswerProblemByAnswerProblemId(answerProblem.getAnswerId());
            for (AnswerProblem answerAnswer: answer) {
                User user = userService.getUserId(answerAnswer.getUserId());
                answerAnswer.setUser(user);
            }
            answerProblem.setAnswer(answer);
            User user = userService.getUserId(answerProblem.getUserId());
            answerProblem.setUser(user);
        }
        if (problem != null){
            map.put("data", problem);
            map.put("msg","success");
            return map;
        }else{
            return map.put("msg","fail");
        }
    }

    @PostMapping("getAllAnswerProblem")
    @ResponseBody
    public Object getAllAnswerProblem(int page,int limit) {
        Map map = new HashMap();
        PageHelper.startPage(page,limit);
        List<AnswerProblem> problem = answerProblemService.selectAllAnswerProblem();
        for (AnswerProblem answerProblem: problem) {
            List<AnswerProblem> answer = answerProblemService.selectAnswerProblemByAnswerProblemId(answerProblem.getAnswerId());
            for (AnswerProblem answerAnswer: answer) {
                User user = userService.getUserId(answerAnswer.getUserId());
                answerAnswer.setUser(user);
            }
            answerProblem.setAnswer(answer);
            User user = userService.getUserId(answerProblem.getUserId());
            answerProblem.setUser(user);
            Commodity commodity = commodityService.getCommodityId(answerProblem.getCommodityId());
            answerProblem.setCommodity(commodity);
        }
        Page<AnswerProblem> pageList = (Page<AnswerProblem>) problem;
        long count = pageList.toPageInfo().getTotal();
        map.put("code",0);
        map.put("msg", "这是数据传来的！");
        map.put("count", count);
        map.put("data", pageList);
        return map;
    }
    @PostMapping("addAnswerProblem")
    @ResponseBody
    public String addAnswerProblem(AnswerProblem answerProblem){
        int count = answerProblemService.addAnswerProblem(answerProblem);
        if (count == 1){
            return "success";
        }else {
            return "fail";
        }
    }
    @PostMapping("updateAnswerProblem")
    @ResponseBody
    public String updateAnswerProblem(AnswerProblem answerProblem){
        int count = answerProblemService.updateAnswerProblem(answerProblem);
        if (count == 1){
            return "success";
        }else {
            return "fail";
        }
    }

    @PostMapping("getAllByAnswerContent")
    @ResponseBody
    public Object getAllByAnswerContent(int page,int limit,String answerContent) {
        Map map = new HashMap();
        PageHelper.startPage(page,limit);
        List<AnswerProblem> problem = answerProblemService.getAllByAnswerProblemContent(answerContent);
        for (AnswerProblem answerProblem: problem) {
            List<AnswerProblem> answer = answerProblemService.selectAnswerProblemByAnswerProblemId(answerProblem.getAnswerId());
            for (AnswerProblem answerAnswer: answer) {
                User user = userService.getUserId(answerAnswer.getUserId());
                answerAnswer.setUser(user);
            }
            answerProblem.setAnswer(answer);
            User user = userService.getUserId(answerProblem.getUserId());
            answerProblem.setUser(user);
            Commodity commodity = commodityService.getCommodityId(answerProblem.getCommodityId());
            answerProblem.setCommodity(commodity);
        }
        Page<AnswerProblem> pageList = (Page<AnswerProblem>) problem;
        long count = pageList.toPageInfo().getTotal();
        map.put("code",0);
        map.put("msg", "这是数据传来的！");
        map.put("count", count);
        map.put("data", pageList);
        return map;
    }
}
